Skip to content

Pyjoyment.Plugin.CORS: Cross-Origin Resource Sharing - #640

Merged
dex4er merged 1 commit into
security-headers-reportsfrom
cors-plugin
Nov 18, 2026
Merged

dex4er merged 1 commit into
security-headers-reportsfrom
cors-plugin

Conversation

@dex4er

@dex4er dex4er commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Part of #634, its fifth step, stacked on #639.

Changes

  • New plugin Pyjoyment.Plugin.CORS, with no counterpart in Mojolicious:
    • In before_dispatch it answers a preflight request (OPTIONS with Access-Control-Request-Method) with 204, before the routes, so a preflight never reaches an action, also on an any route. A preflight from an origin that is not allowed gets 204 without the CORS headers. An OPTIONS request that is not a preflight still reaches the routes.
    • In after_dispatch it adds Access-Control-Allow-Origin (with Access-Control-Allow-Credentials and Access-Control-Expose-Headers when configured) to the responses of actions, static files and error pages, for a request whose Origin is allowed. A header that the action has set is kept.
    • Options:
      • origins: * by default; a list of origins (case-insensitive) or of regular expressions, or a function, as in AllowedHosts
      • methods: GET, HEAD, POST, PUT, PATCH and DELETE by default
      • headers: by default the headers that the preflight asks for are allowed
      • expose, credentials, max_age
      • paths: path prefixes or regular expressions, so CORS can be limited to an API
    • credentials=True with * raises when the plugin is registered: browsers reject it, and reflecting any origin with credentials would let any site read the user's data.
    • Vary: Origin is on every response, within paths, when the origins are not *, and Vary: Access-Control-Request-Headers is on a preflight answer when it echoes the requested headers.
  • The docs say that, with AllowedHosts, the CORS origins must also be among the AllowedHosts origins. This is a deviation from Security headers: HSTS, CSP with nonce and reports, Permissions-Policy, CORS #634, which proposed taking the defaults from AllowedHosts: that plugin does not expose its configuration, so the origins are given explicitly.
  • The plugin is listed in Pyjoyment.Plugins and in the guide's index, has an API page, and is on pyright's strict list.

Validation

  • t/Pyjoyment/cors_lite_app.py:
    • * on the response of an action, a static file and a 404
    • a preflight request reaches no action (on an any route), and on an unknown path gets 204
    • an OPTIONS request that is not a preflight reaches the action
    • origins as a list, a regex or a function, with credentials, expose, methods, headers and max_age
    • an origin that is not allowed gets no CORS headers, also on a preflight
    • Vary: Origin without duplicates
    • paths, including that /apix is not under /api
    • together with AllowedHosts
    • the errors of the options
  • t/docs/Pyjoyment_Plugin_CORS.py: the examples of the documentation
  • pyright strict: 0 errors
  • PYTHONDEVMODE=1 python -m Pyjo.Prove -l -j 4 -r t on asyncio and on Pyjo.Reactor.Poll: all tests pass
  • sphinx-build -W passes
  • trunk check: no issues
  • CI

🤖 Generated with Claude Code

The plugin answers a preflight request before the routes and adds the
headers of CORS to the responses of the actions and the static files, for
the origins, the methods, the headers and the paths of its options, with
Vary: Origin for origins of their own, and never * with credentials.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dex4er dex4er added this to the 0.4.1 milestone Nov 18, 2026
@dex4er
dex4er added this pull request to stack #637 November 18, 2026 17:17
@github-actions

Copy link
Copy Markdown
Contributor

Docker Smoke Test

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
smoke (app) 11 11 0 0 ✅ 134987775481
smoke (fastcgi) 5 5 0 0 ✅ 134987775271
smoke (kubernetes) 8 8 0 0 ✅ 134987774759
smoke (lambda) 2 2 0 0 ✅ 134987775477
smoke (lambda-image) 5 5 0 0 ✅ 134987775111
smoke (lambda-stream) 2 2 0 0 ✅ 134987775281
smoke (lambda-zip) 9 9 0 0 ✅ 134987775385
smoke (lite) 4 4 0 0 ✅ 134987775101
smoke (lite-pep723) 4 4 0 0 ✅ 134987775318
All 50 50 0 0 ✅

The run of the workflow

@github-actions

Copy link
Copy Markdown
Contributor

Tests

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
prove (python 3.12, Asyncio) 7513 7484 29 0 ✅ 134987772581
prove (python 3.12, Poll) 7485 7454 31 0 ✅ 134987772388
prove (python 3.13, Asyncio) 7513 7484 29 0 ✅ 134987772423
prove (python 3.13, Poll) 7485 7454 31 0 ✅ 134987772565
prove (python 3.14, Asyncio) 7544 7520 24 0 ✅ 134987772329
prove (python 3.14, Poll) 7516 7490 26 0 ✅ 134987771918
prove (python 3.15, Asyncio) 7544 7520 24 0 ✅ 134987772414
prove (python 3.15, Poll) 7516 7490 26 0 ✅ 134987772718
prove (python 3.15t, Asyncio) 7544 7520 24 0 ✅ 134987772616
prove (python 3.15t, Poll) 7516 7490 26 0 ✅ 134987772652
prove (python mise, Asyncio) 7623 7588 35 0 ✅ 134987771943
prove (python mise, Asyncio, Windows) 7344 7278 66 0 ✅ 134987772247
prove (python mise, Poll) 7595 7558 37 0 ✅ 134987771473
prove (python mise, Poll, Windows) 7316 7248 68 0 ✅ 134987771969
prove (python pypy3.12, Asyncio) 7513 7484 29 0 ✅ 134987772684
prove (python pypy3.12, Poll) 7485 7454 31 0 ✅ 134987772655
pytest (python 3.12, ubuntu-26.04-arm) 446 420 26 0 ✅ 134987772210
pytest (python 3.15, ubuntu-26.04-arm) 446 425 21 0 ✅ 134987772178
pytest (python 3.15, windows-2025) 446 397 49 0 ✅ 134987772289
servers (python 3.12) 54 54 0 0 ✅ 134987772175
servers (python 3.15) 53 53 0 0 ✅ 134987772019
All 121497 120865 632 0 ✅

The run of the workflow

@read-the-docs-community

Copy link
Copy Markdown

@dex4er dex4er modified the milestones: 0.4.1, 0.5.0 Nov 18, 2026
@dex4er
dex4er merged commit 6d8039a into main Nov 18, 2026
36 checks passed
@dex4er
dex4er deleted the cors-plugin branch November 18, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant